//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.Research.DataLayer
{
    public partial class ConnectionManagerDialog_Base : UserControl
    {
        internal delegate void PropertyChange(bool changed);
        internal event ConnectionManagerDialog.SaveHandler OnSave;
        internal event PropertyChange OnPropertyChange;
        private ProviderPkgInfo provInfo;
        private ConnectionManagerBase connMgr;

        public ConnectionManagerDialog_Base()
        {
            InitializeComponent();

            this.OnPropertyChange += new PropertyChange(ConnectionManagerDialog_Base_OnPropertyChange);
        }

        void ConnectionManagerDialog_Base_OnPropertyChange(bool changed)
        {
            bool hasItems = (this.Presets.Count > 0);
            this.savePreset.Enabled = changed && hasItems && (this.Presets.CurrentPreset != null);
            this.savePresetAs.Enabled = changed || hasItems;
            this.deletePreset.Enabled = hasItems;
            this.Presets.Enabled = hasItems;
        }

        internal void LoadSettings()
        {
            // Convert preset to dictionary
            this.Presets.LoadPresets(provInfo.Presets);
        }

        public ConnectionManagerBase ConnectionManager
        {
            get { return this.connMgr; }
            set { this.connMgr = value; }
        }

        public Connection GetConnectionByID(Guid id)
        {
            IConnMgrInfoParams ds = Presets.GetPresetByID(id);
            if (ds == null)
                return null;
            Presets.CurrentPreset = ds;
            return Connection;
        }

        public ProviderPkgInfo Provider
        {
            get { return provInfo; }
            set { provInfo = value; }
        }

        private IConnMgrInfoParams CurrentPreset
        {
            get
            {
                return this.Presets.CurrentPreset;
            }
        }

        public virtual Connection Connection
        {
            get
            {
                // Need to override
                throw new NotImplementedException();
            }
        }

        public virtual string DisplayStatus
        {
            get
            {
                // Need to override
                throw new NotImplementedException();
            }
        }

        protected void FireOnSave(ConnectionManagerDialog.ConnSaveAction action)
        {
            if (OnSave != null)
                OnSave(action);

            // After saving the property is current
            FireOnPropertyChange(false);
        }

        protected void FireOnPropertyChange(bool changed)
        {
            if (OnPropertyChange != null)
                OnPropertyChange(changed);
        }

        private void ConnectionManagerDialog_Base_Load(object sender, EventArgs e)
        {
            FireOnPropertyChange(false);
        }

        private void deletePreset_Click(object sender, EventArgs e)
        {
            this.Presets.DeletePreset();
            FireOnSave(ConnectionManagerDialog.ConnSaveAction.SavePresets);
        }

        private void savePresetAs_Click(object sender, EventArgs e)
        {
            ConnMgrDlg_Save saveDlg = new ConnMgrDlg_Save();
            if (saveDlg.ShowDialog(this) == DialogResult.OK)
            {
                if (this.connMgr != null && this.connMgr.IsPresetNameExists(saveDlg.SaveAs))
                {
                    MessageBox.Show(Properties.Resources.DuplicatePresetMessage,
                      Properties.Resources.DuplicatePresetTitle,
                      MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
                else
                {
                    // Perform save
                    ProviderPkgInfo prov;
                    IConnMgrInfoParams createNew;

                    CreateNewPreset(saveDlg.SaveAs, out createNew, out prov);

                    this.Presets.AddPreset(createNew);

                    SaveItem();
                }
            }
        }

        protected virtual void SaveItem()
        {
            FireOnSave(ConnectionManagerDialog.ConnSaveAction.SavePresets);
        }

        protected virtual void CreateNewPreset(string name,
            out IConnMgrInfoParams newPreset, out ProviderPkgInfo provider)
        {
            throw new NotImplementedException();
        }

        private void newPreset_Click(object sender, EventArgs e)
        {
            this.Presets.CurrentPreset = null;
        }

        public virtual bool CanTestConnection()
        {
            throw new NotImplementedException();
        }

        public virtual void TestConnection()
        {
            throw new NotImplementedException();
        }

        public void TestConnection(Connection testConn)
        {
            try
            {
                testConn.Open();
                testConn.Close();

                MessageBox.Show("Connection established successfully", "Connection OK",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (BackendStorageException ex)
            {
                MessageBox.Show(string.Format("Cannot establish connection\n\nError: {0}{1}",
                    ex.Message,
                    (ex.InnerException != null ? string.Format("\n\n{0}", ex.InnerException.Message) : "")), 
                    "ERROR", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        public virtual void AddPreset(IConnMgrInfoParams connInfo)
        {
            throw new NotImplementedException();
        }
    }
}
